# clean environment
rm(list=ls())

# load packages
library(ggplot2)
library(readstata13)

# load data set with parliamentary inquiries
dat <- read.csv("inquiries.csv")

# recode extremism variable
dat$extremism_reg <- NA
dat$extremism_reg[dat$extremism_v=="left"] <- 0
dat$extremism_reg[dat$extremism_v=="both"] <- 1
dat$extremism_reg[dat$extremism_v=="right"] <- 2
dat$extremism_reg <- as.numeric(as.character(dat$extremism_reg))

# english names of parties
dat$party_eng <- NA
dat$party_eng[dat$party=="AFD"] <- "AfD"
dat$party_eng[dat$party=="CDU"] <- "CDU/CSU"
dat$party_eng[dat$party=="FDP"] <- "FDP"
dat$party_eng[dat$party=="GRUENE"] <- "Greens"
dat$party_eng[dat$party=="LINKE"] <- "Left"
dat$party_eng[dat$party=="SPD"] <- "SPD"
dat$party_eng <- factor(dat$party_eng, levels=c("CDU/CSU", "AfD", "FDP", "Greens", "Left", "SPD"))

# create decade variable
dat$decade <- NA
dat$decade[dat$year<1960]<-1950
dat$decade[dat$year>=1960 & dat$year<1970]<-1960
dat$decade[dat$year>=1970 & dat$year<1980]<-1970
dat$decade[dat$year>=1980 & dat$year<1990]<-1980
dat$decade[dat$year>=1990 & dat$year<2000]<-1990
dat$decade[dat$year>=2000 & dat$year<2010]<-2000
dat$decade[dat$year>=2010 & dat$year<2020]<-2010

# create jurisdiction variable for merging
dat$jurisdiction2 <- dat$jurisdiction
dat$jurisdiction <- NA
dat$jurisdiction[dat$jurisdiction2=="bb"] <- "Brandenburg"
dat$jurisdiction[dat$jurisdiction2=="be"] <- "Berlin"
dat$jurisdiction[dat$jurisdiction2=="bt"] <- "Bund"
dat$jurisdiction[dat$jurisdiction2=="bw"] <- "Baden-Wurttemberg"
dat$jurisdiction[dat$jurisdiction2=="by"] <- "Bayern"
dat$jurisdiction[dat$jurisdiction2=="hb"] <- "Bremen"
dat$jurisdiction[dat$jurisdiction2=="he"] <- "Hessen"
dat$jurisdiction[dat$jurisdiction2=="hh"] <- "Hamburg"
dat$jurisdiction[dat$jurisdiction2=="mv"] <- "Mecklenburg-Vorpommern"
dat$jurisdiction[dat$jurisdiction2=="ni"] <- "Niedersachsen"
dat$jurisdiction[dat$jurisdiction2=="nw"] <- "Nordrhein-Westfalen"
dat$jurisdiction[dat$jurisdiction2=="rp"] <- "Rheinland-Pfalz"
dat$jurisdiction[dat$jurisdiction2=="sh"] <- "Schleswig-Holstein"
dat$jurisdiction[dat$jurisdiction2=="sl"] <- "Saarland"
dat$jurisdiction[dat$jurisdiction2=="sn"] <- "Sachsen"
dat$jurisdiction[dat$jurisdiction2=="st"] <- "Sachsen-Anhalt"
dat$jurisdiction[dat$jurisdiction2=="th"] <- "Thuringen"

# merge with far-right polling data
polbar <- read.dta13("polbar_agg.dta")
dat <- merge(dat, polbar, by=c("jurisdiction", "year"), all.x = T) 

# regressions
mod_1<-lm(extremism_reg~factor(party_eng)*polbar_fr_vote_pct, data=dat)
mod_2<-lm(extremism_reg~factor(party_eng)*polbar_fr_vote_pct + factor(jurisdiction), data=dat)
mod_3<-lm(extremism_reg~factor(party_eng)*polbar_fr_vote_pct + factor(jurisdiction) + factor(decade), data=dat)
mod_4<-lm(extremism_reg~factor(party_eng)*polbar_fr_vote_pct + factor(jurisdiction) + factor(year), data=dat)
